/*
 *   SSChartDate.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Mon Feb 24 2003.
//
//SSChartDate adds an isEstimated flag to the standard NSCalendarDate. This is primarily so that users
//can change whether it is tentative or not through editing ~ within text fields.


#import "SSChartDate.h"


@implementation SSChartDate

- (id)initWithString:(NSString *)description calendarFormat:(NSString *)format isEstimated:(BOOL)b
{
   if (self = [super initWithString:description calendarFormat:format]) {
      [self setIsEstimated:b];
   }
   return self;
}

+ (id)chartDateWithCalendarDate:(NSCalendarDate *)date isEstimated:(BOOL)b
{
   SSChartDate *returnDate = [[[SSChartDate alloc] initWithString:[NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@/%@", [date descriptionWithCalendarFormat:@"%Y"],
      [date descriptionWithCalendarFormat:@"%m"], [date descriptionWithCalendarFormat:@"%d"], [date descriptionWithCalendarFormat:@"%H"],
      [date descriptionWithCalendarFormat:@"%M"], [date descriptionWithCalendarFormat:@"%S"], [date descriptionWithCalendarFormat:@"%F"]]
                                               calendarFormat:@"%Y/%m/%d/%H/%M/%S/%F" isEstimated:b] autorelease];
   
   return returnDate;
}

- (id)copyWithZone:(NSZone *)zone
{
   SSChartDate *copy = [super copyWithZone:zone];

   //set the isEstimated flag
   [copy setIsEstimated:[self isEstimated]];

   return copy;
}

- (BOOL)isEstimated
{
   return isEstimated;
}

- (void)setIsEstimated:(BOOL)b
{
   isEstimated = b;
}

//***********************************coding
- (void)encodeWithCoder:(NSCoder *)coder
{
   [super encodeWithCoder:coder];

   [coder encodeBool:isEstimated forKey:@"isEstimated"];
}

- (id)initWithCoder:(NSCoder *)coder
{
   if(self = [super initWithCoder:coder]) {
      [self setIsEstimated:[coder decodeBoolForKey:@"isEstimated"]];
   }

   return self;
}

@end
